#include "charge_manager.h"

namespace application {
namespace charge {

ChargeManager::ChargeManager(int floor, std::shared_ptr<kernel::ILogger> logger, QObject* parent)
    : QObject(parent)
    , m_floor(floor)
    , m_logger(logger)
{
}

void ChargeManager::addChargePoint(std::shared_ptr<ChargePoint> chargePoint) {
    if (!chargePoint) {
        if (m_logger) {
            m_logger->warning("尝试添加空充电点对象");
        }
        return;
    }

    QString pointId = chargePoint->getPointId();
    m_chargePoints.insert(pointId, chargePoint);

    if (m_logger) {
        m_logger->debug(QString("充电点已添加: %1 (楼层: %2)").arg(pointId).arg(m_floor));
    }

    // 连接信号，监听充电点可用性变化
    connect(chargePoint.get(), &ChargePoint::occupationChanged, this,
        [this, pointId](bool occupied, int agvId) {
            Q_UNUSED(agvId);
            emit chargePointAvailabilityChanged(pointId, !occupied);
        });

    emit chargePointAdded(pointId);
}

void ChargeManager::removeChargePoint(const QString& pointId) {
    if (m_chargePoints.remove(pointId) > 0) {
        if (m_logger) {
            m_logger->debug(QString("充电点已移除: %1").arg(pointId));
        }
        emit chargePointRemoved(pointId);
    }
}

std::shared_ptr<ChargePoint> ChargeManager::getChargePoint(const QString& pointId) const {
    return m_chargePoints.value(pointId, nullptr);
}

bool ChargeManager::hasChargePoint(const QString& pointId) const {
    return m_chargePoints.contains(pointId);
}

QList<QString> ChargeManager::getAllChargePointIds() const {
    return m_chargePoints.keys();
}

std::shared_ptr<ChargePoint> ChargeManager::findAvailableChargePoint(int agvNumber) const {
    for (auto it = m_chargePoints.constBegin(); it != m_chargePoints.constEnd(); ++it) {
        const auto& point = it.value();
        if (!point) {
            continue;
        }
        const bool occupiedByOther =
            point->isOccupied() &&
            (agvNumber == -1 || point->getCurrentOccupyingAgvId() != agvNumber);
        if (occupiedByOther) {
            continue;
        }
        const bool preOccupiedByOther =
            point->isPreOccupied() &&
            (agvNumber == -1 || point->getPreOccupyingAgvId() != agvNumber);
        if (preOccupiedByOther) {
            continue;
        }
        if (agvNumber != -1 && !point->isVehicleAllowed(agvNumber)) {
            continue;
        }
        return point;
    }
    return nullptr;
}

QList<std::shared_ptr<ChargePoint>> ChargeManager::getAvailableChargePoints(int agvNumber) const {
    QList<std::shared_ptr<ChargePoint>> availablePoints;
    for (auto it = m_chargePoints.constBegin(); it != m_chargePoints.constEnd(); ++it) {
        const auto& point = it.value();
        if (!point) {
            continue;
        }
        const bool occupiedByOther =
            point->isOccupied() &&
            (agvNumber == -1 || point->getCurrentOccupyingAgvId() != agvNumber);
        if (occupiedByOther) {
            continue;
        }
        const bool preOccupiedByOther =
            point->isPreOccupied() &&
            (agvNumber == -1 || point->getPreOccupyingAgvId() != agvNumber);
        if (preOccupiedByOther) {
            continue;
        }
        if (agvNumber != -1 && !point->isVehicleAllowed(agvNumber)) {
            continue;
        }
        availablePoints.append(point);
    }
    return availablePoints;
}

void ChargeManager::initializeChargePoints() {
    if (m_logger) {
        m_logger->info(QString("初始化楼层 %1 的充电点管理器").arg(m_floor));
    }
    // 这里可以从配置或地图数据加载充电点信息
}

} // namespace charge
} // namespace application
